package fu.capstone.hanoicityguide.presentation.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import fu.capstone.hanoicityguide.R;

public class ViewPagerAdapter extends PagerAdapter {
	private Activity activity;
	private ArrayList<String> imageArray = new ArrayList<String>();
	private View imageLayout;
	private ImageView imgViewPager;
	private LayoutInflater inflater;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;

	public ViewPagerAdapter(Activity act, ArrayList<String> imgArra) {
		imageArray = imgArra;
		activity = act;
	}

	public int getCount() {
		int count = 0;
		if (imageArray.size() != 0) {
			for (int i = 0; i < imageArray.size(); i++) {
				if (!"".equals(imageArray.get(i).trim())) {
					count++;
				}
			}
		}
		return count;
	}

	@Override
	public Object instantiateItem(ViewGroup view, int position) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				activity).build();
		if (!ImageLoader.getInstance().isInited()) {
			ImageLoader.getInstance().init(config);
		}
		imageLoader = ImageLoader.getInstance();
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.no_image)
				.showImageOnFail(R.drawable.no_image)
				.resetViewBeforeLoading(true).cacheOnDisk(true)
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
				.bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true)
				.displayer(new FadeInBitmapDisplayer(500)).build();

		imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);

		assert imageLayout != null;
		imgViewPager = (ImageView) imageLayout.findViewById(R.id.image);
		final ProgressBar spinner = (ProgressBar) imageLayout
				.findViewById(R.id.loading);

		imageLoader.displayImage(imageArray.get(position), imgViewPager,
				options, new SimpleImageLoadingListener() {
					@Override
					public void onLoadingStarted(String imageUri, View view) {
						spinner.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						String message = null;
						switch (failReason.getType()) {
						case IO_ERROR:
							message = "Input/Output error";
							break;
						case DECODING_ERROR:
							message = "Image can't be decoded";
							break;
						case NETWORK_DENIED:
							message = "Downloads are denied";
							break;
						case OUT_OF_MEMORY:
							message = "Out Of Memory error";
							break;
						case UNKNOWN:
							message = "Unknown error";
							break;
						}
						// Toast.makeText(MainActivity.this, message,
						// Toast.LENGTH_SHORT).show();
						Log.e("Loading image error!", message);
						spinner.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						spinner.setVisibility(View.GONE);
					}
				});

		view.addView(imageLayout, 0);
		return imageLayout;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == ((View) arg1);
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

}
